package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wx.mall.operate.dto.DiscountDto;
import com.wx.mall.operate.entity.Discount;
import com.wx.mall.operate.mapper.DiscountMapper;
import com.wx.mall.operate.service.IDiscountService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-13
 */
@Service
public class DiscountServiceImpl extends ServiceImpl<DiscountMapper, Discount> implements IDiscountService {


    @Override
    public Integer getId(String disName) {

        QueryWrapper<Discount> wrapper=new QueryWrapper<>();
        wrapper.eq("dis_name",disName);
        Discount discount=this.getOne(wrapper);


        return discount.getDisId();
    };


    @Override
    public List<Discount> getList(DiscountDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        QueryWrapper<Discount> wrapper=new QueryWrapper<>();

        if(!dto.getDisName().equals("")){

            wrapper.like("dis_name",dto.getDisName());
        };

        if(dto.getDisType()!=4){

            wrapper.eq("dis_type",dto.getDisType());
        };

        switch (dto.getSort()){

            case 0:
                wrapper.orderByAsc("end_time");
                break;
            case 1:
                wrapper.orderByAsc("state");
                break;
        }

        wrapper.eq("is_delete",1);
List<Discount> list=this.list(wrapper);
        return list;
    }
}
